package com.jn.gsondemo.views;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.jn.gsondemo.R;

import org.json.JSONArray;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HotSearchActivity extends Activity {
    private ListView hotSearchListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hot_search);

        // 初始化UI元素
        hotSearchListView = findViewById(R.id.hotSearchListView);

        // 解析JSON数据
        List<Map<String, String>> hotSearchList = parseJson();

        // 设置适配器
        SimpleAdapter adapter = new SimpleAdapter(
                this,
                hotSearchList,
                R.layout.hot_search_item,
                new String[]{"title"},
                new int[]{R.id.titleTextView}
        );

        hotSearchListView.setAdapter(adapter);

        // 设置点击事件
        hotSearchListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Map<String, String> clickedItem = (Map<String, String>) parent.getItemAtPosition(position);
                String link = clickedItem.get("link");
                if (link != null && !link.isEmpty()) {
                    // 跳转到浏览器
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(link));
                    startActivity(intent);
                }
            }
        });
    }

    private List<Map<String, String>> parseJson() {
        // 读取assets文件夹中的hotsearch.json文件
        String json = readJsonFromAsset("hotsearch.json");
        List<Map<String, String>> hotSearchList = new ArrayList<>();

        try {
            JSONArray hotSearchArray = new JSONArray(json);
            for (int i = 0; i < hotSearchArray.length(); i++) {
                JSONObject hotSearchObject = hotSearchArray.getJSONObject(i);
                String title = hotSearchObject.getString("title");
                String link = hotSearchObject.getString("link");

                Map<String, String> map = new HashMap<>();
                map.put("title", title);
                map.put("link", link);
                hotSearchList.add(map);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return hotSearchList;
    }

    private String readJsonFromAsset(String fileName) {
        StringBuilder builder = new StringBuilder();
        try {
            InputStream is = getAssets().open(fileName);
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            String line;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return builder.toString();
    }
}